package com.fifth.apartment.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.apartment.constant.MessageConstant;
import com.fifth.apartment.entity.UserInfo;
import com.fifth.apartment.result.Result;
import com.fifth.apartment.result.Results;
import com.fifth.apartment.service.UserInfoService;
import com.fifth.apartment.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author Guzhenghui
 * @since 2024/7/13
 */
@Api(tags = "用户接口")
@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/admin/user")
public class UserInController {
    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "分页查询用户信息")
    @GetMapping("findPage")
    public Result findPage(PageQueryVO vo){
        try {
            Page<UserInfo> page = userInfoService.findPage(vo);
            if(page!=null){
                return new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(true , MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @Operation(summary = "根据用户id更新账号状态")
    @PostMapping("updateStatusById")
    public Results updateStatusById(@RequestParam Long id, @RequestParam Long status) {
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserInfo::getId, id);
        updateWrapper.set(UserInfo::getStatus, status);
        userInfoService.update(updateWrapper);
        return Results.ok();
    }
}
